/*
 * song.c
 * Project 4
 * Nicole Ang (nang1@asu.edu)
 * Bijan Fakhri (bfakhri@asu.edu)
 * CSE 325: Embedded Microprocessor Systems
 * Spring 2013
 */

#include "song.h"

song playThis;
int play;

void song_init(){
	play = 1;//indicate whether a note should be played or not
	playThis.position = 0;

	// Fur Elise
	playThis.notes[0] = createNote(E5, sixteenth);
	playThis.notes[1] = createNote(D5S, sixteenth);
	playThis.notes[2] = createNote(E5, sixteenth);
	playThis.notes[3] = createNote(D5S, sixteenth);
	playThis.notes[4] = createNote(E5, sixteenth);
	playThis.notes[5] = createNote(B4, sixteenth);
	playThis.notes[6] = createNote(D5, sixteenth);
	playThis.notes[7] = createNote(C5, sixteenth);
	playThis.notes[8] = createNote(A4, eighth); 
	playThis.notes[9] = createNote(pause, sixteenth);
	playThis.notes[10] = createNote(C4, sixteenth);
	playThis.notes[11] = createNote(E4, sixteenth);
	playThis.notes[12] = createNote(A4, sixteenth);
	playThis.notes[13] = createNote(B4, sixteenth);
	playThis.notes[14] = createNote(pause, sixteenth);
	playThis.notes[15] = createNote(E4, sixteenth);
	playThis.notes[16] = createNote(G4S, sixteenth);
	playThis.notes[17] = createNote(B4, sixteenth);
	playThis.notes[18] = createNote(C5, sixteenth);
	playThis.notes[19] = createNote(pause, sixteenth);
	playThis.notes[20] = createNote(E4, sixteenth);
	playThis.notes[21] = createNote(E5, sixteenth);
	playThis.notes[22] = createNote(D5S, sixteenth);
	playThis.notes[23] = createNote(E5, sixteenth);
	playThis.notes[24] = createNote(D5S, sixteenth);
	playThis.notes[25] = createNote(E5, sixteenth);
	playThis.notes[26] = createNote(B4, sixteenth);
	playThis.notes[27] = createNote(D5, sixteenth);
	playThis.notes[28] = createNote(C5, sixteenth);
	playThis.notes[29] = createNote(A4, eighth);
	playThis.notes[30] = createNote(pause, sixteenth);
	playThis.notes[31] = createNote(C4, sixteenth);
	playThis.notes[32] = createNote(E4, sixteenth);
	playThis.notes[33] = createNote(A4, sixteenth);
	playThis.notes[34] = createNote(B4, eighth);
	playThis.notes[35] = createNote(pause, sixteenth);
	playThis.notes[36] = createNote(E4, sixteenth);
	playThis.notes[37] = createNote(C5, sixteenth);
	playThis.notes[38] = createNote(B4, sixteenth);
	playThis.notes[39] = createNote(A4, eighth);
	playThis.notes[40] = createNote(pause, eighth);
	playThis.notes[41] = createNote(E5, sixteenth);
	playThis.notes[42] = createNote(D5S, sixteenth);
	playThis.notes[43] = createNote(E5, sixteenth);
	playThis.notes[44] = createNote(D5S, sixteenth);
	playThis.notes[45] = createNote(E5, sixteenth);
	playThis.notes[46] = createNote(B4, sixteenth);
	playThis.notes[47] = createNote(D5, sixteenth);
	playThis.notes[48] = createNote(C5, sixteenth);
	playThis.notes[49] = createNote(A4, eighth);
	playThis.notes[50] = createNote(pause, sixteenth);
	playThis.notes[51] = createNote(C4, sixteenth);
	playThis.notes[52] = createNote(E4, sixteenth);
	playThis.notes[53] = createNote(A4, sixteenth);
	playThis.notes[54] = createNote(B4, eighth);
	playThis.notes[55] = createNote(pause, sixteenth);
	playThis.notes[56] = createNote(E4, sixteenth);
	playThis.notes[57] = createNote(G4S, sixteenth);
	playThis.notes[58] = createNote(B4, sixteenth);
	playThis.notes[59] = createNote(C5, eighth);
	playThis.notes[60] = createNote(pause, sixteenth);
	playThis.notes[61] = createNote(E4, sixteenth);
	playThis.notes[62] = createNote(E5, sixteenth);
	playThis.notes[63] = createNote(D5S, sixteenth);
	playThis.notes[64] = createNote(E5, sixteenth);
	playThis.notes[65] = createNote(D5S, sixteenth);
	playThis.notes[66] = createNote(E5, sixteenth);
	playThis.notes[67] = createNote(B4, sixteenth);
	playThis.notes[68] = createNote(D5, sixteenth);
	playThis.notes[69] = createNote(C5, sixteenth);
	playThis.notes[70] = createNote(A4, eighth);
	playThis.notes[71] = createNote(pause, sixteenth);
	playThis.notes[72] = createNote(C4, sixteenth);
	playThis.notes[73] = createNote(E4, sixteenth);
	playThis.notes[74] = createNote(A4, sixteenth);
	playThis.notes[75] = createNote(B4, eighth);
	playThis.notes[76] = createNote(pause, sixteenth);
	playThis.notes[77] = createNote(E4, sixteenth);
	playThis.notes[78] = createNote(C5, sixteenth);
	playThis.notes[79] = createNote(B4, sixteenth);
	playThis.notes[80] = createNote(A4, eighth);
	playThis.notes[81] = createNote(pause, sixteenth);
	playThis.notes[82] = createNote(B4, sixteenth);
	playThis.notes[83] = createNote(C5, sixteenth);
	playThis.notes[84] = createNote(D5, sixteenth);
	playThis.notes[85] = createNote(E5, eighth_dot);
	playThis.notes[86] = createNote(G4, sixteenth);
	playThis.notes[87] = createNote(F5, sixteenth);
	playThis.notes[88] = createNote(E5, sixteenth);
	playThis.notes[89] = createNote(D5, eighth_dot);
	playThis.notes[90] = createNote(F4, sixteenth);
	playThis.notes[91] = createNote(E5, sixteenth);
	playThis.notes[92] = createNote(D5, sixteenth);
	playThis.notes[93] = createNote(C5, eighth_dot);
	playThis.notes[94] = createNote(E4, sixteenth);
	playThis.notes[95] = createNote(D5, sixteenth);
	playThis.notes[96] = createNote(C5, sixteenth);
	playThis.notes[97] = createNote(B4, eighth);
	playThis.notes[98] = createNote(pause, sixteenth);
	playThis.notes[99] = createNote(E4, sixteenth);
	playThis.notes[100] = createNote(E5, sixteenth);
	playThis.notes[101] = createNote(E4, sixteenth);
	playThis.notes[102] = createNote(E5, sixteenth);
	playThis.notes[103] = createNote(E5, sixteenth);
	playThis.notes[104] = createNote(E6, sixteenth);
}

void playNextNote(){
	if(play){
		int temp = playThis.position;
		Note nextNote = playThis.notes[temp];
		
		//set speaker to play note
		set_frequency(nextNote.pitch);
		
		//set how long the note should be played
		setDuration(nextNote.dur, quarterNoteTime);
		
		//turn on an led
		led_on();
		
		playThis.position = temp + 1;

		// Resets to begining of song
		if(playThis.position > 104)
			playThis.position = 0;
		
		play = 0;
		
	} 
	else{//should pause between notes
		set_frequency(pause);
		setTempo(0);
		play = 1;
	
	} 
}